package edu.ihs.lhpy.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import edu.ihs.lhpy.base.BaseAction;
import edu.ihs.lhpy.domain.Institute;
import edu.ihs.lhpy.domain.PageBean;
import edu.ihs.lhpy.domain.Teacher;
@Controller
@Scope("prototype")
public class TeacherAction extends BaseAction<Teacher>{

	private Long instituteId;
	//列表
	public String list() throws Exception{
		PageBean pageBean = teacherService.getPageBean(pageNum);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "list";
	}
	//添加
	public String add() throws Exception{
		model.setInstitute(instituteService.getById(instituteId));
		teacherService.save(model);
		return "toList";
	}
	//添加界面
	public String addUI() throws Exception{
		List<Institute> instituteList = instituteService.findAll();
		ActionContext.getContext().put("instituteList", instituteList);
		return "addUI";
	}
	//删除
	public String delete() throws Exception{
		teacherService.delete(model.getId());
		return "toList";
	}
	//修改
	public String edit() throws Exception{
		//取出原状态对象
		Teacher teacher = teacherService.getById(model.getId());
		teacher.setName(model.getName());
		teacher.setBirthDay(model.getBirthDay());
		teacher.setDetail(model.getDetail());
		teacher.setShotdetail(model.getShotdetail());
		teacher.setEdutionBackground(model.getEdutionBackground());
		teacher.setEmail(model.getEmail());
		teacher.setGender(model.getGender());
		teacher.setHeadImage(model.getHeadImage());
		teacher.setPhoneNum(model.getPhoneNum());
		teacher.setPosition(model.getPosition());
		teacher.setResearchDirection(model.getResearchDirection());
		teacher.setInstitute(instituteService.getById(instituteId));
		//更新到数据库中
		teacherService.update(teacher);
		return "toList";
	}
	//修改界面
	public String editUI() throws Exception{
		//准备回显数据
		List<Institute> instituteList = instituteService.findAll();
		ActionContext.getContext().put("instituteList", instituteList);
		Teacher teacher = teacherService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(teacher);
		return "editUI";
	}
	
	public Long getInstituteId() {
		return instituteId;
	}
	public void setInstituteId(Long instituteId) {
		this.instituteId = instituteId;
	}
	
}
